package top.jle.wx.proxy.facade.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import top.jle.jpa.basic.util.ExceptionUtils;
import top.jle.wx.proxy.domain.templateMsg.TemplateMessage;
import top.jle.wx.proxy.exception.ExceptionCode;
import top.jle.wx.proxy.facade.TemplateMsgFacade;
import top.jle.wx.proxy.feign.ResultCheckUtil;
import top.jle.wx.proxy.feign.templateMsg.IndustryAPIFeign;
import top.jle.wx.proxy.feign.templateMsg.TemplateMsgAPIFeign;
import top.jle.wx.proxy.util.GlobalThreadLocalUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import top.jle.wx.proxy.constant.TemplateMsgOption;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Set;
import java.util.UUID;

@Component("templateMsgFacade")
public class TemplateMsgFacadeImpl implements TemplateMsgFacade {

    private static final Logger LOGGER = LoggerFactory.getLogger(TemplateMsgFacadeImpl.class);

    @Autowired
    private IndustryAPIFeign industryAPIFeign;
    @Autowired
    private TemplateMsgAPIFeign templateMsgAPIFeign;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private BatchSender batchSender;

    @Override
    public void setInventory(Integer primaryInventoryNumber, Integer secondInventoryNumber){
        if(primaryInventoryNumber==null || secondInventoryNumber ==null){
            ExceptionUtils.throwInvalidParamException();
        }

        String optionSrr= "{\"industry_id1\":"+primaryInventoryNumber+",\"industry_id2\":"+secondInventoryNumber+"}";
        JSONObject option = JSONObject.parseObject(optionSrr);
        JSONObject result = industryAPIFeign.setIndustry(option);
        if(!ResultCheckUtil.check(result)){
            ExceptionUtils.throwException(ExceptionCode.SET_INDUSTRY_FAIL);
        }

    }


    @Override
    public TemplateMsgOption.IndustryResponse getIndustry(){
        JSONObject result = industryAPIFeign.getIndustry();
        if(!ResultCheckUtil.check(result)){
            ExceptionUtils.throwException(ExceptionCode.GET_INDUSTRY_FAIL);
        }
        return result.toJavaObject(TemplateMsgOption.IndustryResponse.class);
    }


    @Override
    public String sendTemplateMsg(TemplateMessage option){
        if(option==null || !option.valid()){
            ExceptionUtils.throwInvalidParamException();
        }
        JSONObject result = templateMsgAPIFeign.send((JSONObject) JSON.toJSON(option));
        LOGGER.info(result.toJSONString());
        if(!ResultCheckUtil.check(result)){
            ExceptionUtils.throwException(ExceptionCode.SEND_TEMPLATE_MSG_FAIL);
        }
        return result.getString("msgid");
    }
    @Override
    public String sendTemplateMsg_batch(Set<String> openids, TemplateMessage option){
        if(CollectionUtils.isEmpty(openids) || option==null || !option.valid()){
            ExceptionUtils.throwInvalidParamException();
        }
        byte[] b= String.valueOf(System.currentTimeMillis()).getBytes(StandardCharsets.UTF_8);
        String queueId = UUID.nameUUIDFromBytes(b).toString();
        batchSender.batchSend(openids,option,queueId, GlobalThreadLocalUtil.getAccessToken());
        return queueId;


    }



    @Override
    public Set<String> getMsgIds(String queueId){
       return stringRedisTemplate.opsForSet().members("msg_queue_"+queueId);
    }

    @Override
    public void sendTemplateMsgToMiniprogram(TemplateMessage option){
        if(option==null || !option.valid()){
            ExceptionUtils.throwInvalidParamException();
        }
        JSONObject result = templateMsgAPIFeign.sendToMiniprogram((JSONObject) JSON.toJSON(option));
        if(!ResultCheckUtil.check(result)){
            ExceptionUtils.throwException(ExceptionCode.SEND_TEMPLATE_MSG_FAIL);
        }

    }
}
